package com.cloud.sysmana.entity.dao.impl;

import java.util.List;

import com.cloud.sysmana.entity.SysUserRole;
import com.cloud.sysmana.entity.SysUserRoleId;
import com.cloud.sysmana.entity.dao.ISysUserRoleDao;
import com.cloud.util.base.BaseHibernateDao;

/**
 * 用户角色关联Dao
 * @author gaof
 * @date 2017年1月4日
 */
public class SysUserRoleDao extends BaseHibernateDao<SysUserRole, SysUserRoleId> implements ISysUserRoleDao {

	/**
	 * 通过用户id获取用户权限
	 * @param userId
	 * @return
	 */
	@Override
	public List<SysUserRole> getUserRolesByUserId(Integer userId) {
		return this.getEntities("FROM SysUserRole WHERE id.userId = " + userId);
	}

	/**
	 * 通过用户id删除用户授权
	 * @param mid
	 */
	@Override
	public void delUserRoleByUserId(int mid) {
		this.executeQuery("delete from sys_user_role where user_id = " + mid);
	}

	/**
	 * 批量添加用户权限
	 * @param userRoles
	 */
	@Override
	public void addUserRoles(List<SysUserRole> userRoles) {
		this.updateEntities(userRoles);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List getUserRoleByUserId(Integer userId) {
		// TODO Auto-generated method stub
		String sqlString="select sr.role_no from sys_role sr where sr.role_id="
				+ " (select sur.role_id from sys_user_role sur "
				+ " where sur.user_id="+userId
				+ " )";
		return this.getSQLQuery(sqlString);
	}

}
